#import "sprite_game_enemy_boss2_skull.h"

@implementation SpriteGameEnemyBoss2Skull

@synthesize flagRotate = flagRotate_;
@synthesize flagWeakPoint = flagWeakPoint_;

- (id)initSpriteGameEnemySkull
{
	NSString *_Path;

	if ((self = [super init]))
	{
		[self schedule:@selector(tick:)];
		flagRotate_ = false;
		flagWeakPoint_ = false;
		[self CreateBody];
		_Path = [NSString stringWithUTF8String:g_CfgGameBoss2Data.skull_image_path];
		image_ = [[CCSprite alloc] initWithSpriteFrameName:_Path];
		[self addChild:image_];
		[image_ release];
		_Path = [NSString stringWithUTF8String:g_CfgGameBoss2Data.weak_point_image_path];
		weakPointFlag_ = [[CCSprite alloc] initWithSpriteFrameName:_Path];
		[self addChild:weakPointFlag_];
		[weakPointFlag_ release];
		[weakPointFlag_ runAction:[CCHide action]];
		self.scale = 0.7f;
	}
	return self;
}

- (void)dealloc
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
	}
	[super dealloc];
}

- (void)tick:(ccTime)Time
{
	CGPoint _Point;

	if (flagRotate_)
	{
		_Point = ptRotatePoint_CGPoint(self.positionInPixels, 1.0f);
		self.positionInPixels = _Point;
		if (NULL != box2dBody_)
		{
			box2dBody_->SetTransform([self ComputeBodyPosition], 0.0f);
		}
	}
}

- (void)tickSkullWeakPointTimeOver:(ccTime)Time
{
	sprite_game_enemy *_Parent;

	[self unschedule:@selector(tickSkullWeakPointTimeOver:)];
	_Parent = (sprite_game_enemy *)self.parent;
	[_Parent CallBackBoss2WeakPointTimeOver:self];
	[self StopActionWeakPoint];
}

- (void)CreateBody
{
	int i;
	int _VertexCount;
	float _Ratio;
	b2Vec2 *_Vertex;
	b2BodyDef _BodyDef;
	b2PolygonShape _PolygonShape;
	b2FixtureDef _FixtureDef;

	_BodyDef.type = b2_dynamicBody;
	box2dBody_ = CMgrGamePhysics::World()->CreateBody(&_BodyDef);
	_Ratio = 1 / CMgrGamePhysics::Ratio();
	_VertexCount = g_CfgGameBoss2Data.box2d_collision_point_table_skull.size();
	_Vertex = ptCreateCollisionVertex(&g_CfgGameBoss2Data.box2d_collision_point_table_skull);
	for (i = 0; i < _VertexCount; ++i)
	{
		_Vertex[i] = ptScaleVector_same(_Vertex[i], _Ratio);
	}
	free(_Vertex);
	_PolygonShape.Set(_Vertex, _VertexCount);
	_FixtureDef.shape = &_PolygonShape;
	_FixtureDef.density = 1.0f;
	_FixtureDef.isSensor = true;
	fixture_ = box2dBody_->CreateFixture(&_FixtureDef);
}

- (void)DestroyBody
{
	if (NULL != box2dBody_)
	{
		[g_LayerGameObject PutBox2dBodyToDestroyPool:box2dBody_];
		box2dBody_ = NULL;
		fixture_ = NULL;
	}
}

- (b2Vec2)ComputeBodyPosition
{
	return ptConvertCGPoint(ptScalePoint_same(ccpAdd(self.positionInPixels, self.parent.positionInPixels), 1 / CMgrGamePhysics::Ratio()));
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[image_ pauseSchedulerAndActions];
	[weakPointFlag_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[image_ resumeSchedulerAndActions];
	[weakPointFlag_ resumeSchedulerAndActions];
}

- (void)ActionWeakPoint:(float)Time
{
	[weakPointFlag_ runAction:[CCShow action]];
	[weakPointFlag_ runAction:[CCRepeatForever actionWithAction:[CCRotateBy actionWithDuration:1.0f angle:20.0f]]];
	[self schedule:@selector(tickSkullWeakPointTimeOver:) interval:Time];
	flagWeakPoint_ = true;
}

- (void)StopActionWeakPoint
{
	[weakPointFlag_ stopAllActions];
	[self unschedule:@selector(tickSkullWeakPointTimeOver:)];
	[weakPointFlag_ runAction:[CCHide action]];
	flagWeakPoint_ = false;
}

- (void)ActionReturn:(float)Time
{
	id _ActionMove;
	id _ActionScale;
	CCSpawn *_Spawn;

	_ActionMove = [action_move_in_pixel actionWithDuration:Time position:CGPointZero];
	_ActionScale = [CCScaleTo actionWithDuration:Time scale:0.0f];
	_Spawn = [CCSpawn actions:_ActionMove, _ActionScale, nil];
	[self runAction:_Spawn];
}

- (void)ActionOut:(float)Time And:(CGPoint)Position
{
	id _ActionMove;
	id _ActionScale;
	CCSpawn *_Spawn;
	CCCallFuncN *_CallBackOutMoveOver;
	CCSequence *_Seq;

	_ActionMove = [action_move_in_pixel actionWithDuration:Time position:Position];
	_ActionScale = [CCScaleTo actionWithDuration:Time scale:0.7f];
	_Spawn = [CCSpawn actions:_ActionMove, _ActionScale, nil];
	_CallBackOutMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionOutMoveOver:)];
	_Seq = [CCSequence actions:_Spawn, _CallBackOutMoveOver, nil];
	[self runAction:_Seq];
}

- (void)ActionDie
{
	id _ActionMove;
	id _ActionFadeOut;
	id _ActionScale;
	CCSpawn *_Spawn;
	CCCallFuncN *_CallBackOutMoveOver;
	CCSequence *_Seq;

	_ActionMove = [action_move_in_pixel actionWithDuration:1.0f position:CGPointZero];
	_ActionFadeOut = [CCFadeOut actionWithDuration:1.0f];
	_ActionScale = [CCScaleTo actionWithDuration:1.0f scale:0.0f];
	_Spawn = [CCSpawn actions:_ActionMove, _ActionFadeOut, _ActionScale, nil];
	_CallBackOutMoveOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionDie:)];
	_Seq = [CCSequence actions:_Spawn, _CallBackOutMoveOver, nil];
	[self runAction:_Seq];
}

- (void)CallBackActionOutMoveOver:(id)Sender
{
	flagRotate_ = TRUE;
}

- (void)CallBackActionDie:(id)Sender
{
	[parent_ removeChild:self cleanup:TRUE];
}

- (void)CallBackBoss2ActionAppearOver:(id)Sender
{
	[self AiActionStandOver];
}

- (void)CallBackBoss2WeakPointTimeOver:(sprite_game_weak_point *)WeakPoint
{
	switch (flagBoss2Stage_)
	{
	case BOSS_2_STAGE_1:[self ActionBoss2StrongAttack:3]; break;
	case BOSS_2_STAGE_2:[self ActionBoss2StrongAttack:5]; break;
	case BOSS_2_STAGE_3:[self ActionBoss2StrongAttack:7]; break;
	default: break;
	}
}

@end
